/*
* BankApplication
*
* r3
*
* GNU General Public License v3.0
*/

package com.acme.bankapp;

import com.acme.bankapp.domain.account.*;
import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.domain.client.*;
import com.acme.bankapp.service.bank.*;
import com.acme.bankapp.exceptions.*;

public class BankApplication {
	
	private final static String clientExistsExceptionMessage = "Exception: client %s already exists\n";
	private final static String overDraftLimitExceededExceptionMessage = "Exception: tryed to widthraw %.2f, limit %.2f\n";
	private final static String notEnoughFundsExceptionMessage = "Exception: tryed to widthraw %.2f\n";
	
	private static BankService bankService = new BankService();
	
	public static void addClient(Bank bank, Client client, Account... accounts) {
		try {
			bankService.addClient(bank, client, accounts);
		} catch (ClientExistsException clientExistsException) {
			System.out.format(clientExistsExceptionMessage, clientExistsException.getClient().getClientSalutation());
		}
	}
	
	public static void main(String[] args) {
		Bank bank = new Bank();
		
		addClient(bank, new Client("Putin", Gender.MALE), new CheckingAccount(0, 1000000));
		addClient(bank, new Client("Ann", Gender.FEMALE), new SavingsAccount(100), new CheckingAccount(100, 1000));
		addClient(bank, new Client("Ya", Gender.MALE), new CheckingAccount(200, 1000));
		addClient(bank, new Client("Noob", Gender.FEMALE), new SavingsAccount(300));
		addClient(bank, new Client("Putin", Gender.MALE), new SavingsAccount(300));

		modifyBank(bank);
		
		bankService.printAll(bank);
	}
	
	private static void withdraw(Account acc, int amount) {
		try {
			acc.withdraw(amount);
		} catch (OverDraftLimitExceededException overDraftLimitExceeededException) {
			System.out.format(overDraftLimitExceededExceptionMessage, overDraftLimitExceeededException.getAmount(), overDraftLimitExceeededException.getOverDraftLimit());
		} catch (NotEnoughFundsException notEnoughFundsException) {
			System.out.format(notEnoughFundsExceptionMessage, notEnoughFundsException.getAmount());
		}
	}
	
	private static void modifyBank(final Bank bank) {
		withdraw(bank.getClients().get(0).getAccounts().get(0), 100);
		bank.getClients().get(1).getAccounts().get(0).deposit(100);
		withdraw(bank.getClients().get(2).getAccounts().get(0), 25);
		bank.getClients().get(2).getAccounts().get(0).deposit(598);
		withdraw(bank.getClients().get(2).getAccounts().get(0), 478);
		withdraw(bank.getClients().get(3).getAccounts().get(0), 10000);
		withdraw(bank.getClients().get(2).getAccounts().get(0), 10000);
	}
	
}
